package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.*;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyLinRon;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeLinRon;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformMultiPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMaskDataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMaskConvertHandler;
import com.ts.api.module.api.entity.ApiProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 安心分期-临融 掩码全流程 9A
 * 飞书链接：https://qqf25ufh3vl.feishu.cn/wiki/ILtXwQtnDidEoDk1L91cQRKvnAg?from=from_copylink   密码：9&v99365
 *
 * @author WJB
 */
@ApiPlan(productId = "379", type = ApiPlanConvertType.PLAT_PHONE_MASK)
public class ApiPlatLinRonPhoneMaskConvert implements ApiPlatformPhoneMaskConvertHandler {

    private static final String KEY = "YluSZRMsJjUmWo2c";
    private static final String KEY_ID = "axfqq";
    private static final String CHANNEL_ID = "axfqq";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        ApiPlatDataExchangeLinRon data = new ApiPlatDataExchangeLinRon();
        data.setMaskPhone(apiDataExchangeParameter.getPhoneMask());
        data.setChannelPromotionCode(CHANNEL_ID);
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiDataExchangeParameter.getAge()));
        //  性别
        Integer sex = ChooseEq.create()
                .when(apiDataExchangeParameter.getSex(), "1", 2)
                .when(apiDataExchangeParameter.getSex(), "0", 1)
                .end(1);
        data.setSex(sex);
        //  城市名称
        data.setCity(apiDataExchangeParameter.getCity());
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiDataExchangeParameter.getSesameCredit(), "1", 1)
                .when(apiDataExchangeParameter.getSesameCredit(), "2", 2)
                .when(apiDataExchangeParameter.getSesameCredit(), "3", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "4", 4)
                .end(1);
        data.setSesameSeedQuota(zhima);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiDataExchangeParameter.getHbLimit(), "1", 5)
                .when(apiDataExchangeParameter.getHbLimit(), "2", 2)
                .when(apiDataExchangeParameter.getHbLimit(), "3", 3)
                .when(apiDataExchangeParameter.getHbLimit(), "4", 4)
                .when(apiDataExchangeParameter.getHbLimit(), "5", 4)
                .end(5);
        data.setAntCreditPayQuota(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiDataExchangeParameter.getBtLimit(), "1", 5)
                .when(apiDataExchangeParameter.getBtLimit(), "2", 2)
                .when(apiDataExchangeParameter.getBtLimit(), "3", 3)
                .when(apiDataExchangeParameter.getBtLimit(), "4", 4)
                .when(apiDataExchangeParameter.getBtLimit(), "5", 4)
                .end(5);
        data.setJdBtQuota(btLimit);
        //  微粒贷
        data.setMicroLoanQuota(1);
        //  信用情况
        Integer overdue = ChooseEq.create()
                .when(apiDataExchangeParameter.getOverdue(), "1", 1)
                .when(apiDataExchangeParameter.getOverdue(), "2", 2)
                .when(apiDataExchangeParameter.getOverdue(), "3", 2)
                .end(2);
        data.setBeOverdue(overdue);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiDataExchangeParameter.getCreditLimit(), "1", 2)
                .when(apiDataExchangeParameter.getCreditLimit(), "2", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "3", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "4", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "5", 1)
                .when(apiDataExchangeParameter.getCreditLimit(), "6", 1)
                .end(2);
        data.setCreditCard(creditCard);
        //  学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiDataExchangeParameter.getEducationAttainment(), "1", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "2", 3)
                .when(apiDataExchangeParameter.getEducationAttainment(), "3", 2)
                .when(apiDataExchangeParameter.getEducationAttainment(), "4", 4)
                .when(apiDataExchangeParameter.getEducationAttainment(), "5", 5)
                .when(apiDataExchangeParameter.getEducationAttainment(), "6", 6)
                .end(1);
        data.setEducationLevel(educationAttainment);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 2)
                .end(3);
        data.setHouseLevel(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 3)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 2)
                .end(3);
        data.setVehicleLevel(vehicle);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 3)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 1)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 2)
                .end(3);
        data.setAccumulationFundLevel(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 3)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 1)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 2)
                .end(3);
        data.setSocialSecurityLevel(socialSecurity);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiDataExchangeParameter.getInsurancePolicy(), "1", 3)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "2", 2)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "3", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "4", 2)
                .end(3);
        data.setInsure(commericalInsurance);
        //  职业
        Integer career = ChooseEq.create()
                .when(apiDataExchangeParameter.getCareer(), "1", 1)
                .when(apiDataExchangeParameter.getCareer(), "2", 8)
                .when(apiDataExchangeParameter.getCareer(), "3", 3)
                .when(apiDataExchangeParameter.getCareer(), "4", 5)
                .when(apiDataExchangeParameter.getCareer(), "5", 4)
                .end(9);
        data.setOccupation(career);
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiDataExchangeParameter.getMonthlySalary(), "1", 1)
                .when(apiDataExchangeParameter.getMonthlySalary(), "2", 2)
                .when(apiDataExchangeParameter.getMonthlySalary(), "3", 3)
                .when(apiDataExchangeParameter.getMonthlySalary(), "4", 4)
                .when(apiDataExchangeParameter.getMonthlySalary(), "5", 5)
                .end(1);
        data.setOccupationMonthlyIncome(monthlySalary);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanAmount(), "1", 30000)
                .when(apiDataExchangeParameter.getLoanAmount(), "2", 50000)
                .when(apiDataExchangeParameter.getLoanAmount(), "3", 100000)
                .when(apiDataExchangeParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);
        //  借款期限
        Integer time = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanTerm(), "1", 1)
                .when(apiDataExchangeParameter.getLoanTerm(), "2", 2)
                .when(apiDataExchangeParameter.getLoanTerm(), "3", 4)
                .when(apiDataExchangeParameter.getLoanTerm(), "4", 5)
                .when(apiDataExchangeParameter.getLoanTerm(), "5", 5)
                .when(apiDataExchangeParameter.getLoanTerm(), "6", 5)
                .end(1);
        data.setLoanPeriod(time);
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("accessKeyId", KEY_ID);
            newMap.put("uid", apiDataExchangeParameter.getReqId());
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyLinRon data = new ApiPlatApplyLinRon();
        data.setCid(apiApplyParameter.getApplyId());
        data.setChannelPromotionCode(CHANNEL_ID);
        data.setName(apiApplyParameter.getUserName());
        data.setPhone(apiApplyParameter.getPhone());
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  性别
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "1", 2)
                .when(apiApplyParameter.getSex(), "0", 1)
                .end(1);
        data.setSex(sex);
        //  ip
        if (ValidParam.isNotEmpty(apiApplyParameter.getIp())) {
            data.setClientIp(apiApplyParameter.getIp());
        } else {
            data.setClientIp("127.0.0.1");
        }
        //  城市名称
        data.setCity(apiApplyParameter.getCity());
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 1)
                .when(apiApplyParameter.getSesameCredit(), "2", 2)
                .when(apiApplyParameter.getSesameCredit(), "3", 3)
                .when(apiApplyParameter.getSesameCredit(), "4", 4)
                .end(1);
        data.setSesameSeedQuota(zhima);
        //  花呗
        Integer huaBei = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", 5)
                .when(apiApplyParameter.getHbLimit(), "2", 2)
                .when(apiApplyParameter.getHbLimit(), "3", 3)
                .when(apiApplyParameter.getHbLimit(), "4", 4)
                .when(apiApplyParameter.getHbLimit(), "5", 4)
                .end(5);
        data.setAntCreditPayQuota(huaBei);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", 5)
                .when(apiApplyParameter.getBtLimit(), "2", 2)
                .when(apiApplyParameter.getBtLimit(), "3", 3)
                .when(apiApplyParameter.getBtLimit(), "4", 4)
                .when(apiApplyParameter.getBtLimit(), "5", 4)
                .end(5);
        data.setJdBtQuota(btLimit);
        //  微粒贷
        data.setMicroLoanQuota(1);
        //  信用情况
        Integer overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 1)
                .when(apiApplyParameter.getOverdue(), "2", 2)
                .when(apiApplyParameter.getOverdue(), "3", 2)
                .end(2);
        data.setBeOverdue(overdue);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 2)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 1)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(2);
        data.setCreditCard(creditCard);
        //  学历
        Integer educationAttainment = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 3)
                .when(apiApplyParameter.getEducationAttainment(), "3", 2)
                .when(apiApplyParameter.getEducationAttainment(), "4", 4)
                .when(apiApplyParameter.getEducationAttainment(), "5", 5)
                .when(apiApplyParameter.getEducationAttainment(), "6", 6)
                .end(1);
        data.setEducationLevel(educationAttainment);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 2)
                .end(3);
        data.setHouseLevel(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 3)
                .when(apiApplyParameter.getCarInfo(), "2", 2)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 2)
                .end(3);
        data.setVehicleLevel(vehicle);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 3)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 2)
                .end(3);
        data.setAccumulationFundLevel(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 3)
                .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                .when(apiApplyParameter.getSocialSecurity(), "4", 2)
                .end(3);
        data.setSocialSecurityLevel(socialSecurity);
        //  商业保险
        Integer commericalInsurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 3)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 2)
                .end(3);
        data.setInsure(commericalInsurance);
        //  职业
        Integer career = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 8)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 5)
                .when(apiApplyParameter.getCareer(), "5", 4)
                .end(9);
        data.setOccupation(career);
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 1)
                .when(apiApplyParameter.getMonthlySalary(), "2", 2)
                .when(apiApplyParameter.getMonthlySalary(), "3", 3)
                .when(apiApplyParameter.getMonthlySalary(), "4", 4)
                .when(apiApplyParameter.getMonthlySalary(), "5", 5)
                .end(1);
        data.setOccupationMonthlyIncome(monthlySalary);
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);
        //  借款期限
        Integer time = ChooseEq.create()
                .when(apiApplyParameter.getLoanTerm(), "1", 1)
                .when(apiApplyParameter.getLoanTerm(), "2", 2)
                .when(apiApplyParameter.getLoanTerm(), "3", 4)
                .when(apiApplyParameter.getLoanTerm(), "4", 5)
                .when(apiApplyParameter.getLoanTerm(), "5", 5)
                .when(apiApplyParameter.getLoanTerm(), "6", 5)
                .end(1);
        data.setLoanPeriod(time);
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("accessKeyId", KEY_ID);
            newMap.put("uid", apiApplyParameter.getReqId());
            newMap.put("data", dataStr);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMaskApply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMaskApply.ofFail();
        }
        return RespPlatformPhoneMaskApply.ofSuc();
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }

    @Override
    public RespPlatformMultiPhoneMaskDataExchange respDataExchange(String respJson) {
        //  撞库返回响应体判断
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }

        JSONObject resData = jsonObject.getJSONObject("data");
        if (ValidParam.isEmpty(resData)) {
            return RespPlatformMultiPhoneMaskDataExchange.ofFail();
        }

        RespPlatformMultiPhoneMaskDataExchange respPlatformMultiPhoneMaskDataExchange = RespPlatformMultiPhoneMaskDataExchange.ofSuc();
        List<RespPlatformPhoneMaskDataExchange> listRespPlatformPhoneMaskDataExchange = CollUtils.newArrayList();
        if (ValidParam.isNotNull(resData)) {
            String productName = resData.getString("productName");
            String orderId = resData.getString("cid");

            List<ApiProtocol> protocolList = new ArrayList<>();
            //  获取协议
            JSONArray listProtocolResult = resData.getJSONArray("protocol");
            IteratorUtils.array(listProtocolResult, (protocolResult, index) -> {
                JSONObject protocolObject = listProtocolResult.getJSONObject(index);
                ApiProtocol protocol = new ApiProtocol();
                protocol.setProtocolName(protocolObject.getString("protocolName"));
                protocol.setProtocolUrl(protocolObject.getString("protocolUrl"));
                protocolList.add(protocol);
            });

            List<String> phoneMd5List = new ArrayList<>();
            JSONArray jsonArray = resData.getJSONArray("md5List");
            IteratorUtils.array(jsonArray, (item, index) -> {
                String phoneMd5 = jsonArray.getString(index);
                phoneMd5List.add(phoneMd5);
            });

            RespPlatformPhoneMaskDataExchange result = RespPlatformPhoneMaskDataExchange.of();
            result.setDisplayName(productName);
            result.setProductName(productName);
            result.setApplyId(orderId);
            result.setProtocolList(protocolList);
            result.setListPhoneMd5(phoneMd5List);
            listRespPlatformPhoneMaskDataExchange.add(result);
        }
        respPlatformMultiPhoneMaskDataExchange.setListPhoneMask(listRespPlatformPhoneMaskDataExchange);
        return respPlatformMultiPhoneMaskDataExchange;
    }
}
